package cn.sciento.farm.devicemanager.gateway.downlink.service.impl;


import cn.sciento.farm.devicemanager.common.constant.MqttConstant;
import cn.sciento.farm.devicemanager.gateway.downlink.service.MqttCallBackFuture;
import cn.sciento.farm.devicemanager.gateway.model.downlink.AbstractBlockData;
import cn.sciento.farm.devicemanager.gateway.model.downlink.InverterControlData;
import cn.sciento.farm.devicemanager.gateway.model.inverter.InverterRealData;
import cn.sciento.farm.devicemanager.gateway.uplink.service.inverter.InverterDisposeService;
import cn.sciento.farm.devicemanager.sensor.Inverter;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

@Order(3)
@Service(value = "inverterSendDataService")
public class InverterDataService implements InverterDisposeService {


    @Override
    public void changeOnline(String eui, int online) {

    }

    @Override
    public void changeParams(InverterRealData data) {
        MqttCallBackFuture mqttCallBackFuture = MqttCallBackFuture.futurePool.get(
                String.format(
                        MqttConstant.INVERTER_CONTROL_BLOCK_ID,
                        data.getEui()
                )
        );
        if (mqttCallBackFuture == null) {
            return;
        }
        if (data.isStatusData()) {
            AbstractBlockData blockData = mqttCallBackFuture.getAbstractBlockData();
            if (blockData instanceof InverterControlData) {
                InverterControlData controlData = (InverterControlData) blockData;
                if (controlData.getVfdControl() != null) {
                    if (data.getStatus() == 1) {
                        if (controlData.getVfdControl() == 1 || controlData.getVfdControl() == 3
                                || controlData.getVfdControl() == 8) {
                            mqttCallBackFuture.setResponse("");

                        }
                    } else if (data.getStatus() == 2) {
                        if (controlData.getVfdControl() == 2 || controlData.getVfdControl() == 4) {
                            mqttCallBackFuture.setResponse("");
                        }
                    } else if (data.getStatus() == 3) {
                        if (controlData.getVfdControl() == 5 || controlData.getVfdControl() == 6) {
                            mqttCallBackFuture.setResponse("");
                        }
                    } else {
                        mqttCallBackFuture.setResponse("");
                    }

                }
                if(controlData.getGivenPress() != null || controlData.getHyperPress() != null
                || controlData.getLowPress() != null) {
                    if(data.getGivenPress() != null || data.getHyperPress() != null
                            || data.getLowPress() != null){
                        mqttCallBackFuture.setResponse("");
                    }
                }

            } else {
                mqttCallBackFuture.setResponse("");
            }

        }
    }
}
